package org.yjc.controller;

import java.util.Random;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yjc.custom.JqgridParam;
import org.yjc.custom.JqgridUtils;
import org.yjc.entity.Eim;
import org.yjc.entity.EndUser;
import org.yjc.entity.KingRoot;
import org.yjc.form.EimForm;
import org.yjc.security.UserUtils;
import org.yjc.service.EimService;
import org.yjc.service.EndUserService;
import org.yjc.service.LesseeService;

@Controller
@RequestMapping(value="/eimrefer")
public class EimReferController {
	@Autowired
	private EndUserService enduserservice;
	@Autowired
	private EimService eimservice;
	@Autowired
	private LesseeService lesseeservice;
	@Autowired
	UserUtils userutils; //获取当前用户工具类
	
	@RequestMapping(value="/eimrefer")
	private String lesseeall(ModelMap map) {
		Integer enduserid = userutils.getUser().getId();//获取当前用户的id
		EndUser enduser = enduserservice.findById(enduserid); //获取到当前用户对象
		Integer lesseeid = enduser.getLessee().getId(); //获取该用户的上级租户id
		map.put("lesseeid",lesseeid);
		return "eimrefer/refereim";	
	}
	
	@RequestMapping(value="/addeim")
	private String lesseeadd(ModelMap map) {
		Integer enduserid = userutils.getUser().getId();//获取当前用户的id
		EndUser enduser = enduserservice.findById(enduserid); //获取到当前用户对象
		Integer lesseeid = enduser.getLessee().getId(); //获取该用户的上级租户id
		map.put("lesseeid",lesseeid);
		return "eimrefer/addeim";	
	}
	
	@RequestMapping(value="list")
	@ResponseBody
	private Object list(JqgridParam param,Integer lesseeId) {
		Pageable pageable = param.buildPageable();
		Specification spec = new Specification<KingRoot>() {

			@Override
			public Predicate toPredicate(Root<KingRoot> root, CriteriaQuery<?> arg1, CriteriaBuilder cb) {
				Predicate predicate = cb.equal(root.get("lessee").get("id"), lesseeId);
				return predicate;
			}
		};
		Page<Eim> page;
		if(param.getSearch()) {
			spec = spec.and(param.buildSpec());
		}
		page = eimservice.findAll(spec,pageable);
		return JqgridUtils.getPageResult(page);
	}
	
	@RequestMapping(value="edit")
	@ResponseBody
	public Object edit(String id,EimForm form) {
		String oper = form.getOper();
		Eim eim = new Eim();
		if("edit".equals(oper)) {
			eim = eimservice.findById(Integer.parseInt(id));
		}
		Integer i = 10000;
		Integer j = 1000000;
		Random random = new Random();
		Integer s = random.nextInt(j)%(j-i+1) + i; //取随机数
		Integer s1 = random.nextInt(j)%(j-i+1) + i;
		Integer s2 = random.nextInt(j)%(j-i+1) + i;
		String max = String.valueOf(s); //将Integer类型强转为String类型
		String max1 = String.valueOf(s1);
		String max2= String.valueOf(s2);
		BeanUtils.copyProperties(form, eim,"id");
		eim.setInvnumber(max);
		eim.setInvcode(max1);
		eim.setTin(max2);
		eimservice.save(eim);
		return "OK";
	}
}